package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.simtop.annotation.RepeatSubmit;
import com.simtop.domain.LeaveWord;
import com.simtop.service.LeaveWordService;
import com.simtop.utils.ResultBean;
import com.simtop.utils.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @author lvyongwei
 */
@RestController
@RequestMapping("/leaveWord")
public class LeaveWordController extends BaseController {

    @Autowired
    private LeaveWordService leaveWordService;

    /**
     * 添加留言信息
     * @return
     */
    @PostMapping("/insertLeaveWord")
    @RepeatSubmit
    public ResultBean insertLeaveWord(@RequestBody @Validated LeaveWord leaveWord){
        return ResultBean.toResult(leaveWordService.insertLeaveWord(leaveWord));
    }

    /**
     * 查询留言信息
     * @param leaveWord
     * @return
     */
    @PostMapping("/getAllLeaveWord")
    public ResultBean getAllLeaveWord(@RequestBody LeaveWord leaveWord){
        if(leaveWord.getIfPage() != null && leaveWord.getIfPage() == false){
            return ResultBean.success("获取留言成功",leaveWordService.getAllLeaveWord(leaveWord));
        }else{
            startPage(leaveWord);
            List<LeaveWord> allLeaveWord = leaveWordService.getAllLeaveWord(leaveWord);
            return ResultBean.success("获取留言成功",getDataTable(allLeaveWord));
        }
    }

    /**
     * 删除留言信息
     * @param data
     * @return
     */
    @PostMapping("/deleteLeaveWord")
    public ResultBean deleteLeaveWord(@RequestBody @NotBlank(message = "删除id不能为空") String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        String leaveWordIds = jsonObject.getString("leaveWordIds");
        List<Long> ids = Arrays.asList(Convert.toLongArray(leaveWordIds));
        return ResultBean.toResult(leaveWordService.deleteLeaveWord(ids));
    }
}